package com.qianfeng.project.fragment;

import java.util.Date;

import org.apache.http.Header;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.project.R;
import com.qianfeng.project.global.BaseFragment;
import com.qianfeng.project.model.callback.BaseCallback;
import com.qianfeng.project.model.response.DataResp;
import com.qianfeng.project.model.webservice.ApiProvider;
import com.qianfeng.project.model.webservice.UrlConstants;
import com.qianfeng.tools.MyToast;
import com.qianfeng.tools.Utils;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SearchFragment extends BaseFragment implements android.view.View.OnClickListener {
	@ViewInject(R.id.btn_search)
	private Button btn_search;
	@ViewInject(R.id.btn_priceDate)
	private Button btn_priceDate;
	@ViewInject(R.id.llayout_city)
	private LinearLayout llayout_city;
	@ViewInject(R.id.llayout_occupancy_out)
	private LinearLayout llayout_occupancy_out;
	@ViewInject(R.id.llayout_check_out)
	private LinearLayout llayout_check_out;
	@ViewInject(R.id.tv_city)
	private TextView tv_city;
	@ViewInject(R.id.tv_occupancy_date)
	private TextView tv_occupancy_date;
	@ViewInject(R.id.tv_check_out_date)
	private TextView tv_check_out_date;
	@ViewInject(R.id.tv_occupancy_week)
	private TextView tv_occupancy_week;
	@ViewInject(R.id.tv_check_out_week)
	private TextView tv_check_out_week;
	
	private String occupancy;// 居住
	private String occupancy_date;// 居住日期(月/日)
	private String occupancy_week;// 居住周
	private String check_out;// 退房
	private String check_out_date;// 退房日期
	private String check_out_week;// 退房周
	
	private String doorModel = null;// 房屋类型
	private int nights = 0;

	private DataResp dataResp;
	private int mode = 0;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_search, null,false);
		ViewUtils.inject(this, view); //注入view和事件
		
		llayout_city.setOnClickListener(this);
		llayout_occupancy_out.setOnClickListener(this);
		llayout_check_out.setOnClickListener(this);
		btn_search.setOnClickListener(this);
		btn_priceDate.setOnClickListener(this);
		
		//入住退房日期
		if (occupancy == null || "".equals(occupancy)) {
			Date date = new Date();
			occupancy = date.toString();
			check_out = date.toString();
			
		}
		//设置日期显示
		setSelectData(occupancy, check_out, nights);
		
		return view;
	}
	//设置入住退房日期显示
	//自定义的Utils类和parseDateGetDate和parseDateGetWeek方法
	private void setSelectData(String occupancy2, String check_out2, int nights2) {
		// TODO Auto-generated method stub
		occupancy_date = Utils.parseDateGetDate(occupancy2);
		occupancy_week = Utils.parseDateGetWeek(occupancy2);
		check_out_date = Utils.parseDateGetDate(check_out2);
		check_out_week = Utils.parseDateGetWeek(check_out2);
		tv_occupancy_date.setText(occupancy_date);
		tv_check_out_date.setText(check_out_date);
		tv_occupancy_week.setText(occupancy_week);
		tv_check_out_week.setText(check_out_week);
	}
	
	private void getRoomData(){
		if (!Utils.checkNetworkConnection(getActivity())) {
			MyToast.shortToast(getActivity(), "当前无网络，请检查重试");
			return;
		}
		
		MyToast.shortToast(getActivity(), "正在查询服务器数据");
		ApiProvider.RoomDateCleander(UrlConstants.ROOMDATE, new BaseCallback<DataResp>(DataResp.class) {
			@Override
			public void onSuccess(int statusCode, Header[] headers, DataResp t) {
				// TODO Auto-generated method stub
				btn_priceDate.setClickable(true);
				if (statusCode == 200) {
					
				}
				
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String msg) {
				// TODO Auto-generated method stub
				
			}

		});
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

//	private void jumpToDate() {
//	//	Intent intent = new Intent(getActivity(), DateActivity.class);
//		intent.putExtra("INDATE", occupancy);
//		intent.putExtra("OUTDATE", check_out);
//		Bundle bundle = new Bundle();
//		bundle.putSerializable("ROOMDATE", dataResp);
//		intent.putExtra("ROOMDATE", bundle);
//		startActivityForResult(intent, 1);
//	}
	
}
